package com.meusdrinks.view;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

import org.primefaces.model.chart.PieChartModel;

import br.gov.frameworkdemoiselle.lifecycle.Startup;

import com.meusdrinks.business.DrinkBC;
import com.meusdrinks.business.IngredienteBC;
import com.meusdrinks.business.IngredienteDrinkBC;
import com.meusdrinks.business.UsuarioBC;
import com.meusdrinks.domain.Drink;
import com.meusdrinks.domain.Ingrediente;
import com.meusdrinks.domain.IngredienteDrink;

@ManagedBean
@ViewScoped
public class NovoDrinkMB {
	
	private List<IngredienteDrink> composicao;
	
	private Drink drink;
	
	@Inject
	private IngredienteBC ingredienteBC;
	
	@Inject
	private UsuarioBC usuarioBC;
	
	@Inject
	private DrinkBC drinkBC;
	
	@Inject
	private IngredienteDrinkBC ingredienteDrinkBC;
	
	private Long id;
	
	private PieChartModel chart;
	
	private String tamanho;
	
	private String nomeDrink;
	
	
	@PostConstruct
	private void init(){
		composicao = new ArrayList<IngredienteDrink>();
		drink = new Drink();
		chart = new PieChartModel();

	}
	
	public void adicionarIngrediente(){
		for (IngredienteDrink ingredienteDrink: composicao){
			if (ingredienteDrink.getIngrediente().getId().equals(id)){
				ingredienteDrink.setQuantidade(ingredienteDrink.getQuantidade()+1);
				updateChart();
				return;
			}
		}
		Ingrediente i = ingredienteBC.load(id);
		IngredienteDrink novo = new IngredienteDrink();
		novo.setQuantidade(1);
		novo.setIngrediente(i);
		composicao.add(novo);
		updateChart();
	}
	
	private void updateChart(){
		int total = 0;
		for (IngredienteDrink ingredienteDrink: composicao){
			total += ingredienteDrink.getQuantidade();
		}
		for (IngredienteDrink ingredienteDrink: composicao){
			chart.set(ingredienteDrink.getIngrediente().getNome(), 100*ingredienteDrink.getQuantidade()/total);
		}
	}
	
	public String salvar(){
		for (IngredienteDrink ingDrink: composicao){
			ingredienteDrinkBC.insert(ingDrink);
		}
		drink.setComposicao(composicao);
		drink.setNome(nomeDrink);
		drink.setSomaNotas(0);
		drink.setQtdAvaliacoes(0);
		drink.setCriador(usuarioBC.load(1l));
		drinkBC.insert(drink);
		for (IngredienteDrink ingDrink: composicao){
			ingDrink.setDrink(drink);
			ingredienteDrinkBC.update(ingDrink);
		}
		return "home.jsf";
	}

	public List<IngredienteDrink> getComposicao() {
		return composicao;
	}

	public void setComposicao(List<IngredienteDrink> composicao) {
		this.composicao = composicao;
	}

	public Drink getDrink() {
		return drink;
	}

	public void setDrink(Drink drink) {
		this.drink = drink;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public PieChartModel getChart() {
		return chart;
	}

	public void setChart(PieChartModel chart) {
		this.chart = chart;
	}

	public String getTamanho() {
		return tamanho;
	}

	public void setTamanho(String tamanho) {
		this.tamanho = tamanho;
	}

	public String getNomeDrink() {
		return nomeDrink;
	}

	public void setNomeDrink(String nomeDrink) {
		this.nomeDrink = nomeDrink;
	}
	
	

}
